﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Xiaowei.ViewModel;

namespace Xiaowei.Controls
{
    /// <summary>
    /// ActiveButton.xaml 的交互逻辑
    /// </summary>
    public partial class ActiveButton : UserControl
    {
        public ActiveButton()
        {
            InitializeComponent();
            DataContext = MiniModeViewModel.Instance;
            MiniModeViewModel.Instance.PropertyChanged += Instance_PropertyChanged;
            InitAnimation();
        }

        private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName==nameof(MiniModeViewModel.Instance.SpeechRecognizingText))
            {
                if(MiniModeViewModel.Instance.SpeechRecognizingText!="")
                {
                    Dispatcher.Invoke(()=> {
                        logo.Visibility = Visibility.Collapsed;
                    });
                    
                }
                else
                {
                    Dispatcher.Invoke(() => {
                        logo.Visibility = Visibility.Visible;
                    });
                }
            }
        }

        private void SpeakingTimer_Tick(object sender, EventArgs e)
        {
            MiniModeViewModel.Instance.SpeechRecognizingText="你好，我在听...";
        }

        private bool isListening = false;

        public void SwitchStateToListening()
        {
            if (isListening) return;
            Dispatcher.Invoke(() => {
                if(isShowDetials)
                {
                    PlayExpandAnimation2();
                }
                else
                {
                    PlayExpandAnimation1();
                }
                Visibility = Visibility.Visible;
            });
            isListening = true;
        }
        public void SwitchStateToNormal()
        {
            if (!isListening) return;
            MiniModeViewModel.Instance.SpeechRecognizingText = "";
            Dispatcher.Invoke(() => {
                PlayReduceStoryList1();
            });

            isListening = false;

        }
        private bool isShowDetials = false;
        public void ShowDetials()
        {
            if (isListening)
            {
                return;
            }
            isShowDetials = true;
            Visibility = Visibility.Visible;
        }
        public void HideDetials()
        {
            if (isListening)
            {
                return;
            }
            isShowDetials = false;
            Dispatcher.Invoke(() => {
                Visibility = Visibility.Collapsed;
            });

        }
        public event Action SwitchToNormalEnd;
    }
}
